﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace E1_eMart
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        public TaiKhoan gTaiKhoan = new TaiKhoan();
        protected int loaiTaiKhoan = -1;
        protected List<SanPham> splist = new List<SanPham>();
        protected List<LoaiSanPham> lsplist = new List<LoaiSanPham>();
        protected List<SanPham> topsplist = new List<SanPham>();
        protected string catID = "1";
        protected int page = 1;
        protected int totalpage = 1;
        protected string key = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            catID = Request["catID"];
            key = Request["key"];

            if(Request["page"] != null)
                page = int.Parse(Request["page"]);

            DataClasses1DataContext edata = new DataClasses1DataContext();

            var itemQuery3 = from sp in edata.LoaiSanPhams
                             select sp;
            lsplist = itemQuery3.ToList();

            List<int> idtemplist = new List<int>();
            var itemQuery4 = from ct in edata.ChiTietDauGias
                             group ct by ct.MaSanPham into gr
                             orderby gr.Count() descending
                             select new { id = gr.Key.ToString() };

            foreach (var idtemp in itemQuery4)
            {
                idtemplist.Add(int.Parse(idtemp.id));
            }

            var itemQuery5 = from sp in edata.SanPhams
                             where idtemplist.Contains(sp.MaSanPham)
                             select sp;

            topsplist = itemQuery5.Take(8).ToList();

            if (String.IsNullOrEmpty(Request["catID"]))
            {
                Response.Redirect("index.aspx");
            }

            else
            {
                ((DropDownList)this.Master.FindControl("catSelector")).SelectedIndex = int.Parse(catID);
                
                var itemQuery2 = from sp in edata.SanPhams
                                 where (sp.TenSanPham.Contains(key) || (sp.GiaHienTai.ToString()).Contains(key) ||
                                    (sp.GiaKhoiDiem.ToString()).Contains(key)) && (catID == "0" ? true : sp.MaLoaiSanPham == int.Parse(catID))
                                 select sp;
                splist = itemQuery2.ToList();

                totalpage = (splist.Count - 1) / 8 + 1;
                splist = itemQuery2.Skip(8 * (page - 1)).Take(8).ToList();
            }
            if (Session["username"] != null)
            {
                gTaiKhoan = (TaiKhoan)Session["username"];
                loaiTaiKhoan = (int)gTaiKhoan.MaLoaiTaiKhoan;
            }
        }
    }

    public class StringFormatter : IFormatProvider, ICustomFormatter
    {
        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            return stringConverter(arg.ToString());
        }

        public object GetFormat(Type formatType)
        {
            return (formatType == typeof(ICustomFormatter)) ? this : null;
        }


        protected string stringConverter(string originalString)
        {
            originalString.Replace("á", "a");
            originalString.Replace("à", "a");
            originalString.Replace("ả", "a");
            originalString.Replace("ã", "a");
            originalString.Replace("ạ", "a");

            originalString.Replace("ấ", "a");
            originalString.Replace("ầ", "a");
            originalString.Replace("ẩ", "a");
            originalString.Replace("ẫ", "a");
            originalString.Replace("ậ", "a");

            originalString.Replace("ắ", "a");
            originalString.Replace("ằ", "a");
            originalString.Replace("ẳ", "a");
            originalString.Replace("ẵ", "a");
            originalString.Replace("ặ", "a");

            originalString.Replace("é", "e");
            originalString.Replace("è", "e");
            originalString.Replace("ẻ", "e");
            originalString.Replace("ẽ", "e");
            originalString.Replace("ẹ", "e");

            originalString.Replace("ế", "e");
            originalString.Replace("ề", "e");
            originalString.Replace("ể", "e");
            originalString.Replace("ễ", "e");
            originalString.Replace("ệ", "e");

            originalString.Replace("ú", "u");
            originalString.Replace("ù", "u");
            originalString.Replace("ủ", "u");
            originalString.Replace("ũ", "u");
            originalString.Replace("ụ", "u");

            originalString.Replace("í", "i");
            originalString.Replace("ì", "i");
            originalString.Replace("ỉ", "i");
            originalString.Replace("ĩ", "i");
            originalString.Replace("ị", "i");

            originalString.Replace("ó", "o");
            originalString.Replace("ò", "o");
            originalString.Replace("ỏ", "o");
            originalString.Replace("õ", "o");
            originalString.Replace("ọ", "o");

            originalString.Replace("ố", "o");
            originalString.Replace("ồ", "o");
            originalString.Replace("ổ", "o");
            originalString.Replace("ỗ", "o");
            originalString.Replace("ộ", "o");

            originalString.Replace("ớ", "o");
            originalString.Replace("ờ", "o");
            originalString.Replace("ở", "o");
            originalString.Replace("ỡ", "o");
            originalString.Replace("ợ", "o");

            originalString.Replace("ý", "y");
            originalString.Replace("ỳ", "y");
            originalString.Replace("ỷ", "y");
            originalString.Replace("ỹ", "y");
            originalString.Replace("ỵ", "y");

            originalString.Replace("đ", "d");

            originalString.Replace("Á", "A");
            originalString.Replace("À", "A");
            originalString.Replace("Ả", "A");
            originalString.Replace("Ã", "A");
            originalString.Replace("Ạ", "A");

            originalString.Replace("Ấ", "A");
            originalString.Replace("Ầ", "A");
            originalString.Replace("Ẩ", "A");
            originalString.Replace("Ẫ", "A");
            originalString.Replace("Ậ", "A");

            originalString.Replace("Ắ", "A");
            originalString.Replace("Ằ", "A");
            originalString.Replace("Ẳ", "A");
            originalString.Replace("Ẵ", "A");
            originalString.Replace("Ặ", "A");

            originalString.Replace("É", "E");
            originalString.Replace("È", "E");
            originalString.Replace("Ẻ", "E");
            originalString.Replace("Ẽ", "E");
            originalString.Replace("Ẹ", "E");

            originalString.Replace("Ế", "E");
            originalString.Replace("Ề", "E");
            originalString.Replace("Ể", "E");
            originalString.Replace("Ễ", "E");
            originalString.Replace("Ệ", "E");

            originalString.Replace("Ú", "U");
            originalString.Replace("Ù", "U");
            originalString.Replace("Ủ", "U");
            originalString.Replace("Ũ", "U");
            originalString.Replace("Ụ", "U");

            originalString.Replace("Í", "I");
            originalString.Replace("Ì", "I");
            originalString.Replace("Ỉ", "I");
            originalString.Replace("Ĩ", "I");
            originalString.Replace("Ị", "I");

            originalString.Replace("Ó", "O");
            originalString.Replace("Ò", "O");
            originalString.Replace("Ỏ", "O");
            originalString.Replace("Õ", "O");
            originalString.Replace("Ọ", "O");

            originalString.Replace("Ồ", "O");
            originalString.Replace("Ố", "O");
            originalString.Replace("Ổ", "O");
            originalString.Replace("Ỗ", "O");
            originalString.Replace("Ộ", "O");

            originalString.Replace("Ớ", "O");
            originalString.Replace("Ờ", "O");
            originalString.Replace("Ở", "O");
            originalString.Replace("Ỡ", "O");
            originalString.Replace("Ợ", "O");

            originalString.Replace("Ý", "Y");
            originalString.Replace("Ỳ", "Y");
            originalString.Replace("Ỷ", "Y");
            originalString.Replace("Ỹ", "Y");
            originalString.Replace("Ỵ", "Y");

            originalString.Replace("Đ", "D");

            return originalString;
        }
    }
}